package Dao;

import Entity.Officer;
import Entity.Patient;
import Entity.Room;
import Utils.SimpleTools;
import javafx.scene.control.Alert;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StaffDao extends PatternDao {
    private Officer officer = new Officer();
    private int operator;

    private static StaffDao instance = null;
    private StaffDao(){}
    public static StaffDao getInstance(){
        if(instance == null){
            instance = new StaffDao();
        }
        return instance;
    }

    public int getOperator() {
        return operator;
    }
    public void setOperator(int operator) {
        this.operator = operator;
    }
    public Officer getOfficer() {

        return officer;
    }
    public void setOfficer(Officer officer) {
        this.officer = officer;
    }

    //返回所有病患的集合
    public static List<Patient> getPatients() throws IOException {
        ObjectMapper om = new ObjectMapper();
        File file = new File("files\\Patient.json");
        List<Patient> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Patient>>() {});
        }

        return list;
    }

    //将病患的集合存储进文件
    public static void StorePatients(List<Patient> list) throws IOException {
        File file = new File("files\\Patient.json");
        ObjectMapper om = new ObjectMapper();
        om.writeValue(file,list);
    }


    //根据建筑、楼层、房间号返回房间的集合
    public static List<Room> searchRooms(String building,String floor,String name) throws IOException {
        ObjectMapper om = new ObjectMapper();
        File file = new File("files\\Room.json");
        List<Room> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Room>>() {});
        }
        List<Room> Roomlist = new ArrayList<>();
        if(building!=null){
            if(floor!=null){
                if(name!=null){
                    for(Room room:list) if(room.getBuildinglocation().equals(building)&&room.getFloorlocation().equals(floor)&&room.getName().equals(name)) Roomlist.add(room);
                }
                else{
                    for(Room room:list) if(room.getBuildinglocation().equals(building)&&room.getFloorlocation().equals(floor)) Roomlist.add(room);
                }
            }
            else{
                for(Room room:list) if(room.getBuildinglocation().equals(building)) Roomlist.add(room);
            }
        }
        else{
            SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "至少需要一栋建筑");
            Roomlist = list;
        }
        return Roomlist;
    }
    //返回所有房间楼号的集合
    public static List<String> getRoomBuildings() throws IOException {
        ObjectMapper om = new ObjectMapper();
        File file = new File("files\\Room.json");
        List<Room> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Room>>() {});
        }
        List<String> Roomlist = new ArrayList<>();
        for(Room room:list){
            String str = room.getBuildinglocation();
            if(!Roomlist.contains(str)) Roomlist.add(str);
        }
        return Roomlist;
    }

    //返回所有房间toString的集合
    public static List<String> getRoomstrs() throws IOException {
        ObjectMapper om = new ObjectMapper();
        File file = new File("files\\Room.json");
        List<Room> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Room>>() {});
        }
        List<String> RoomToString = new ArrayList<>();
        for(Room room:list){
            RoomToString.add(room.toString());
        }
        return RoomToString;
    }

    //返回所有房间的集合
    public static List<Room> getRooms() throws IOException {
        ObjectMapper om = new ObjectMapper();
        File file = new File("files\\Room.json");
        List<Room> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Room>>() {});
        }
        return list;
    }
    //将全部房间的集合替换进文件
    public static void storeRooms(List<Room> rooms) throws IOException {
        ObjectMapper om = new ObjectMapper();
        File file = new File("files\\Room.json");
        om.writeValue(file,rooms);
    }
    //添加房间
    public static void addRoom(Room room) throws IOException {
        ObjectMapper om = new ObjectMapper();
        File file = new File("files\\Room.json");
        List<Room> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Room>>() {});
        }

        list.add(room);
        om.writeValue(file,list);
    }
    //将房间在文件中替换
    public static void changeRoom(Room r1,Room r2) throws IOException {
        ObjectMapper om = new ObjectMapper();
        File file = new File("files\\Room.json");
        List<Room> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Room>>() {});
        }

        for(Room r:list){
            if(r.getFloorlocation().equals(r.getFloorlocation())&&r.getName().equals(r1.getName())&&r.getBuildinglocation().equals(r1.getBuildinglocation())) {
//                list.remove(r);
//                list.add(r2);
                list.set(list.indexOf(r),r2);
            }
        }
        om.writeValue(file,list);
    }
    //删除房间
    public static void deleteRoom(Room room) throws IOException {
        ObjectMapper om = new ObjectMapper();
        File file = new File("files\\Room.json");
        List<Room> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Room>>() {});
        }

        for(Room r:list){
            if(r.equals(room)){
                list.remove(room);
                break;
            }
        }
        om.writeValue(file,list);
    }
    public static void deleteRooms(List<Room> rooms) throws IOException {
        ObjectMapper om = new ObjectMapper();
        File file = new File("files\\Room.json");
        List<Room> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Room>>() {});
        }

        for(Room r2:rooms){
            for(Room r1:list){
                if(r2.equals(r1)){
                    list.remove(r1);
                    break;
                }
            }
        }
        om.writeValue(file,list);
    }
}
